package com.woniuxy.controller;

import cn.hutool.core.io.FileUtil;
import com.woniuxy.dto.MyDto;
import com.woniuxy.util.DateUtil;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class UploadController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private String uploadDir;
    @Autowired
    private org.springframework.core.env.Environment environment;

    @PostMapping("/uploadImage2.do")
    public MyDto uploadImage2(@RequestParam("mPositive") MultipartFile mPositive, HttpServletRequest request) {
        //读取设置的图片的文件夹
        String path1=environment.getProperty("file.upload.url1");
        File dirFile = new File(path1);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //获取文件的后缀
        String fileSuffix = mPositive.getOriginalFilename().substring(mPositive.getOriginalFilename().lastIndexOf(".") + 1);
        //构建时间戳
        String fileName = "img_" + DateUtil.timeStamp(new Date()) + "." + fileSuffix;
        try {
            File uploadFile = new File(dirFile, fileName);
            mPositive.transferTo(uploadFile);
      //      FileUtils.copyInputStreamToFile(mPositive.getInputStream(), uploadFile);
            MyDto dto = new MyDto();
            dto.setCode(200);
            dto.setMsg("上传成功");
            Map<String ,Object>  map=new HashMap<>();
            map.put("fileName",fileName);
            map.put("path",path1);
            dto.setData(map);
            return dto;
        } catch (IOException e) {
            e.printStackTrace();
           return new MyDto(-200,"上传图片失败");
        }
    }
    @PostMapping("/uploadImage3.do")
    public MyDto uploadImage3(@RequestParam("mSide") MultipartFile mSide, HttpServletRequest request) {
        //读取设置的图片的文件夹
        String path1=environment.getProperty("file.upload.url2");
        File dirFile = new File(path1);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //获取文件的后缀
        String fileSuffix = mSide.getOriginalFilename().substring(mSide.getOriginalFilename().lastIndexOf(".") + 1);
        logger.info("后缀名:" + fileSuffix);
        //构建时间戳
        String fileName = "img_" + DateUtil.timeStamp(new Date()) + "." + fileSuffix;

        logger.info("文件的路径:" + fileName);
        try {
            File uploadFile = new File(dirFile, fileName);
            mSide.transferTo(uploadFile);
            //      FileUtils.copyInputStreamToFile(mPositive.getInputStream(), uploadFile);
            MyDto dto = new MyDto();
            dto.setCode(200);
            dto.setMsg("上传成功");
            Map<String ,Object>  map=new HashMap<>();
            map.put("fileName",fileName);
            map.put("path",path1);
            dto.setData(map);
            return dto;
        } catch (IOException e) {
            e.printStackTrace();
            return new MyDto(-200,"上传图片失败");
        }
    }

    //显示图片的方法
    @RequestMapping(value = "/showImage")
    public ResponseEntity<Object> showImage(String imageFileName) {
        //创建文件对象
        File file = new File(environment.getProperty("file.upload.url1"), imageFileName);
        if (file.exists()) {
            byte[] bytes = null;
            bytes = FileUtil.readBytes(file);
            //创建封装响应头信息对象
            HttpHeaders headers = new HttpHeaders();
            //封装响应头类型（APPLICATION_OCTET_STREAM 响应的内容不限定）
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            ResponseEntity<Object> entity = new ResponseEntity<Object>(bytes, headers, HttpStatus.CREATED);
            return entity;
        }
        return null;
    }

}
